package com.lj.ljminsu.controller;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.lj.ljminsu.bean.JsonResultVo;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RequestMapping("user")
@RestController
public class PhoneYanController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 手机验证码
     *
     * @param userPhone
     * @return
     */
    @CrossOrigin
    @RequestMapping("securityCode")
    public JsonResultVo securityCode(String userPhone) {
        JsonResultVo jsonResultVo = new JsonResultVo();
        StringBuilder str = new StringBuilder();
        Random random = new Random();
        Pattern p = Pattern.compile("1\\d{10}");
        Matcher m = p.matcher(userPhone);
        boolean b = m.matches();
        if ("".equals(userPhone)||null==userPhone) {
            jsonResultVo.setCode(2);
            jsonResultVo.setMsg("请输入手机号");
            return jsonResultVo;
        }
        if (b) {
                        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                    "LTAImQjdF4iijQiI", "qZWCyozor7dvXSLOY28PnAK94Gg03r");
            IAcsClient client = new DefaultAcsClient(profile);

            CommonRequest request = new CommonRequest();
            request.setMethod(MethodType.POST);
            request.setDomain("dysmsapi.aliyuncs.com");
            request.setVersion("2017-05-25");
            request.setAction("SendSms");
            request.putQueryParameter("RegionId", "cn-hangzhou");
            request.putQueryParameter("PhoneNumbers", userPhone);
            request.putQueryParameter("SignName", "途家");
            request.putQueryParameter("TemplateCode", "SMS_171356523");
            for (int i = 0; i < 4; i++) {
                str.append(random.nextInt(10));
            }
            String num = str.toString();
            stringRedisTemplate.opsForValue().set(userPhone + "phoneSecurityCode", num, 5L, TimeUnit.MINUTES);
            request.putQueryParameter("TemplateParam", "{\"code\":"+num+"}");
            try {
                CommonResponse response = client.getCommonResponse(request);
                System.out.println(response.getData());
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
            jsonResultVo.setCode(0);
            jsonResultVo.setMsg("验证码已发送");
            return jsonResultVo;
        }
            jsonResultVo.setCode(1);
            jsonResultVo.setMsg("手机号码格式不对");
            return jsonResultVo;

    }
}
